<template xmlns="http://www.w3.org/1999/html">
  <div class="content">
    <div class="title">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }"><b>系统</b></el-breadcrumb-item>
        <el-breadcrumb-item>数据维护</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="main-content">
      <el-col>
        <el-autocomplete
          icon="search"
          v-model="state"
          :fetch-suggestions="queryDataDetail" 
          placeholder="请输入内容" 
          @select="handleSelect">
        </el-autocomplete>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <el-button @click="add" type="primary">新增</el-button>
      </el-col>
      <el-col :span="20">
        <el-table :data="accounts" highlight-current-row v-loading="loading" style="width: 100%;margin-left: 50px;margin-top: 30px">
          <el-table-column type="index" label="ID" width="100">
          </el-table-column>
          <el-table-column prop="dataId" label="dataId" v-if="false">
          </el-table-column>
          <el-table-column prop="name" label="名称" width="150">
          </el-table-column>
          <el-table-column prop="mnemonicCode" label="助记码" width="100">
          </el-table-column>
          <el-table-column prop="remark" label="备注" width="250">
          </el-table-column>
          <el-table-column prop="options" label="操作" min-width="100">
              <template scope="scope">
                <el-button @click="changeData(scope.$index,scope.row)" type="text" size="small">修改</el-button>
                <el-button @click="deleteData(scope.$index,scope.row,accounts)" type="text" size="small">删除</el-button>
              </template>
            </el-table-column>
        </el-table>
      </el-col>
    </div>
  </div>
</template>

<script>
  import ElCol from 'element-ui/packages/col/src/col'
  import ElForm from '../../../node_modules/element-ui/packages/form/src/form.vue'
  import axios from 'axios'
  import $ from 'jquery'

  export default {
    components: {
      ElForm,
      ElCol
    },
    data () {
      return {
        state:'',
        timeout:null,
        dataList:[],
        dataGroup:'',
        accounts:[],
        items:[],
        department: '',
        pagesize: 10,
        currentPage:1,
        total: 0,
      }
    },
    methods:{
      createStateFilter(queryString) {
        return (state) => {
          return (state.value.indexOf(queryString.toLowerCase()) === 0);
        };
      },

      queryDataDetail(queryString, cb){
        var results = queryString ? this.dataList.filter(this.createStateFilter(queryString)) : this.dataList;

        clearTimeout(this.timeout);
        this.timeout = setTimeout(() => {
          cb(results);
        }, 3000 * Math.random());
      },

      handleSelect(item){
        console.log(item)
        this.getList(item)
      },
      getAccount:function () {
        this.$http.get("/dataDict/dataGroupList")
        .then((response ) => {
          let that = this;
          $.each(response.data.data,function(index,value){
            that.dataList.push({value:value.name,id:value.code})
            that.items.push({code:value.code,name:value.name});
          });
        })
        .catch(function(error){
          console.log(error)
        })
      },

      getList:function(item){
        this.dataGroup=item.id;
        this.$http.get("/dataDict/"+item.id)
        .then((response) =>{
          this.accounts=response.data.data
          console.log("getList:"+response)
        })
        .catch(function(error){
          console.log(error)
        })
      },

      deleteData:function(index,row,tableData){
        let that = this
        this.$http.delete("/dataDict/"+this.dataGroup+"/"+row.dataId)
        .then((response)=>{
          //tableData.splice(index,1);
          if(response.data.success){
            this.$message("删除成功")
            that.getAccount()
          }
        })
        .catch(function(error){
          console.log(error)
        })
      },

      changeData:function(index,row){
        this.$router.push({path:'/backgroundManaSys/addOrChangeMeta',query:{content:row}})
      },


      moerInfo:function () {
        //this.items.push()
      },

      add:function () {
        this.$router.push({path:'/backgroundManaSys/addOrChangeMeta',query:{group:this.dataGroup}})
      }
    },

    mounted() {
      this.getAccount();
    }
  }
</script>

<style lang="scss" scoped>
.content {
  padding: 10px;
  background: #F7F7F7;
  .title {
    color: #99A9BF;
    font-size: 14px;
    line-height: 18px;
    margin: 8px 0px 10px 0px;
  }
  .main-content {
    background: #fff;
    position: fixed;
    top: 96px;
    bottom: 10px;
    left: 216px;
    right: 16px;
    overflow: auto;
    padding: 20px;
  }
}
</style>